package com.assist.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.assist.dao.IUserDAO;
import com.assist.model.User;
import com.assist.util.HibernateUtil;

public class UserDAOImpl implements IUserDAO {
	
	public void saveOrUpdateUser(User user) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.saveOrUpdate(user);
	}

	public User getUser(String userName, String password) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("userName", userName));
		criteria.add(Restrictions.eq("password", password));
		List results = criteria.list();
		return (User) results.get(0);
	}

	@Override
	public User getUserById(Long userId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("id", userId));
		List results = criteria.list();
		return (User) results.get(0);
	}
}
